package com.leetcode.LeetCode精选TOP面试题;

/**
 * @author 覃国强
 * @date 2022-05-18 17:15
 */
public class 题库_0014_最长公共前缀 {

  static
  class Solution {
    public String longestCommonPrefix(String[] strs) {
      if (strs == null || strs.length == 0) {
        return "";
      }
      StringBuilder answer = new StringBuilder();
      for (int i = 0; i < strs[0].length(); ++i) {
        char ch = strs[0].charAt(i);
        for (int j = 1; j < strs.length; ++j) {
          if (i >= strs[j].length()) {
            return answer.toString();
          }
          if (ch != strs[j].charAt(i)) {
            return answer.toString();
          }
        }
        answer.append(ch);
      }
      return answer.toString();
    }
  }

}
